﻿using System.Text;
using XQ.WeChatOfficialAccount.DAL.Model.Sys;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;

namespace XQ.WeChatOfficialAccount.DAL.DAL
{
    /// <summary>
    /// 按钮与菜单操作类
    /// </summary>
    public class ButtonMenuDal
    {
        private WeChatOfficialAccountEntities ce;

        /// <summary>
        /// 返回所有的按钮
        /// </summary>
        /// <returns></returns>
        public List<Sys_Buttons> GetButtonses()
        {
            using (ce = new WeChatOfficialAccountEntities())
            {
                return ce.Sys_Buttons.ToList();
            }
        }

        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="button"></param>
        /// <returns></returns>
        public string SaveButton(Sys_Buttons button)
        {
            int res = 0;
            if (button.ID == 0)
            {
                using (ce = new WeChatOfficialAccountEntities())
                {

                    if (ce.Sys_Buttons.Where(n => n.ButtonText == button.ButtonText).ToList().Count > 0)
                    {
                        return "按钮名称已存在！";
                    }
                    else
                    {
                        using (var cesave = new WeChatOfficialAccountEntities())
                        {

                            cesave.Sys_Buttons.Add(button);
                            res = cesave.SaveChanges();
                            return res > 0 ? "ok" : "保存失败！";
                        }
                    }

                }

            }
            else
            {
                using (ce = new WeChatOfficialAccountEntities())
                {
                    if (
                        ce.Sys_Buttons
                        .Where(n => n.ButtonText == button.ButtonText && n.ID != button.ID)
                        .ToList().Count >
                        0)
                    {
                        return "按钮名称已存在！";
                    }
                    else
                    {
                        using (var ceupdate = new WeChatOfficialAccountEntities())
                        {
                            var b = ceupdate.Sys_Buttons.First(n => n.ID == button.ID);
                            b.ID = button.ID;
                            b.ButtonText = button.ButtonText;
                            b.Sortnum = button.Sortnum;
                            b.Remark = button.Remark;
                            b.buttonId = button.buttonId;
                            res = ceupdate.SaveChanges();
                            return res > 0 ? "ok" : "保存失败！";
                        }
                    }


                }
            }

        }

        /// <summary>
        /// 删除按钮
        /// </summary>
        /// <param name="id">按钮ID</param>
        /// <returns></returns>
        public bool DeleteButton(int id)
        {
            using (ce = new WeChatOfficialAccountEntities())
            {
                var delButton = ce.Sys_Buttons.First(n => n.ID == id);
                ce.Sys_Buttons.Remove(delButton);
                return ce.SaveChanges() > 0 ? true : false;
            }
        }

        /// <summary>
        /// 获取单个按钮信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Sys_Buttons GetButton(int id)
        {
            using (ce = new WeChatOfficialAccountEntities())
            {
                return ce.Sys_Buttons.FirstOrDefault(n => n.ID == id);
            }
        }

        /// <summary>
        /// 返回菜单页面按钮个数
        /// </summary>
        /// <param name="menuId"></param>
        /// <returns></returns>
        public bool GetMenuButtonsesInfo(int menuId, int btnId)
        {
            using (ce = new WeChatOfficialAccountEntities())
            {
                return ce.Sys_Menu_Buttons.Where(n => n.NavId == menuId && n.ButtonId == btnId).ToList().Count > 0 ? true : false;
            }
        }

        /// <summary>
        /// 保存页面按钮
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <param name="menuId">菜单ID</param>
        /// <param name="btnId">菜单ID</param>
        /// <param name="flag">标记</param>
        public void SaveMenuButtons(int menuId, int btnId, bool flag)
        {
            //添加
            if (flag)
            {
                using (ce = new WeChatOfficialAccountEntities())
                {
                    string sql = @"if not exists (select * from Sys_Menu_Buttons where NavId=@NavId and ButtonId=@ButtonId)
  insert into Sys_Menu_Buttons([NavId] ,[ButtonId],[Sortnum]) values(@NavId,@ButtonId,1)";
                    object[] par =
                    {
                        new SqlParameter("@NavId",menuId),
                        new SqlParameter("@ButtonId",btnId)
                    
                    };
                    ce.Database.ExecuteSqlCommand(sql, par);
                }
            }
            //删除
            else
            {
                using (ce = new WeChatOfficialAccountEntities())
                {
                    var srmb = ce.Sys_Menu_Buttons.First(n => n.NavId == menuId && n.ButtonId == btnId);
                    if (srmb != null)
                        ce.Sys_Menu_Buttons.Remove(srmb);
                    ce.SaveChanges();
                }
            }
        }

        /// <summary>
        /// 根据角色获取所有菜单和按钮
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <returns></returns>
        public List<MenuButtonModel> GetMenuButtons(int roleId)
        {
            using (ce = new WeChatOfficialAccountEntities())
            {
                var sql = new StringBuilder();
                sql.Append("select (select MenuName from Sys_Menus where ID=MenuID) Menu,");
                sql.Append(" MenuID ID,");
                sql.Append("(select ButtonText from Sys_Buttons where ID=BtnId) buttonName,");
                sql.Append("(select buttonId from Sys_Buttons where ID=BtnId) buttonId  from Sys_Role_Menu_Btns where RoleId=@roleId");
                var par = new object[] { new SqlParameter("@roleId", roleId) };
                return ce.Database.SqlQuery<MenuButtonModel>(sql.ToString(), par).ToList();

            }
        }
    }
}
